import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 票据模板类，用于存储和管理票据模板信息
 */
public class BillTemplate implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String templateName = "默认模板";
    private float templateWidth = 210f;  // 毫米
    private float templateHeight = 297f; // 毫米
    private List<FieldInfo> fields = new ArrayList<>();
    
    public BillTemplate() {
    }
    
    public void addField(String name, float x, float y, float width, float height, float fontSize) {
        FieldInfo field = new FieldInfo();
        field.setName(name);
        field.setX(x);
        field.setY(y);
        field.setWidth(width);
        field.setHeight(height);
        field.setFontSize(fontSize);
        fields.add(field);
    }
    
    public void saveToFile(String filename) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(new File(filename), this);
    }
    
    public static BillTemplate loadFromFile(String filename) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(new File(filename), BillTemplate.class);
    }
    
    // Getters and Setters
    public String getTemplateName() {
        return templateName;
    }
    
    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }
    
    public float getTemplateWidth() {
        return templateWidth;
    }
    
    public void setTemplateWidth(float templateWidth) {
        this.templateWidth = templateWidth;
    }
    
    public float getTemplateHeight() {
        return templateHeight;
    }
    
    public void setTemplateHeight(float templateHeight) {
        this.templateHeight = templateHeight;
    }
    
    public List<FieldInfo> getFields() {
        return fields;
    }
    
    public void setFields(List<FieldInfo> fields) {
        this.fields = fields;
    }
    
    /**
     * 字段信息内部类
     */
    public static class FieldInfo implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String name;
        private float x;
        private float y;
        private float width;
        private float height;
        private float fontSize;
        
        // Getters and Setters
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public float getX() {
            return x;
        }
        
        public void setX(float x) {
            this.x = x;
        }
        
        public float getY() {
            return y;
        }
        
        public void setY(float y) {
            this.y = y;
        }
        
        public float getWidth() {
            return width;
        }
        
        public void setWidth(float width) {
            this.width = width;
        }
        
        public float getHeight() {
            return height;
        }
        
        public void setHeight(float height) {
            this.height = height;
        }
        
        public float getFontSize() {
            return fontSize;
        }
        
        public void setFontSize(float fontSize) {
            this.fontSize = fontSize;
        }
    }
}
